package com.maotai.llzhou.timetask;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.llzhou.service.MaotaiService;
import com.maotai.llzhou.thread.MaotaiLoginThread;
import com.maotai.llzhou.util.IPFactory;

public class MaotaiCheckLoginTask {

	@Resource(name = "maotaiService")
	private MaotaiService maotaiService;

	public void beginTask() {
		try {
			List<Map<String, Object>> list = maotaiService
					.getZhanghaoCheckLogin();
			if (list != null && list.size() > 0) {
//				JSONObject jObject = IPFactory.getKdByUrl(IPFactory.ZM_IP_HTTPS_URL_20);
//				JSONArray jArray = jObject.getJSONArray("data");
				JSONArray jArray = IPFactory.getIpAndPort();

				int count = list.size();
				int countData = jArray.size();
				if (countData < count) {
					count = countData;
				}
				for (int i = 0; i < count; i++) {
					Map<String, Object> zhanghao = list.get(i);
					JSONObject jj = (JSONObject) jArray.get(i);
					String ipAndPort = jj.getString("ip") + ":"
							+ jj.getString("port");
					//因为是3个步骤共用一个ip，所以先修改3个状态,然后每个步骤执行过程中出错了，再进行更新状态
					zhanghao.put("loginstatus", "1");
					maotaiService.updateZhanghaoLoginStatus(zhanghao);
//					zhanghao.put("checkstatus", "1");
//					maotaiService.updateZhanghaoCheckStatus(zhanghao);
//					zhanghao.put("yuyuestatus", "1");
//					maotaiService.updateZhanghaoYuYueStatus(zhanghao);
//					
//					MaotaiRegisterLoginThread mrl = new MaotaiRegisterLoginThread(maotaiService, zhanghao, ipAndPort);
//					mrl.run();
					
					
					MaotaiLoginThread mcl = new MaotaiLoginThread(maotaiService, zhanghao, ipAndPort);
					mcl.start();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
